package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired(required = false)
    CategoryService categoryService;

    @PostMapping
    public R add(@RequestBody Category category, HttpSession session){
        category.setCreateUser((Long) session.getAttribute("employee"));
        category.setUpdateUser((Long) session.getAttribute("employee"));
        categoryService.add(category);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R lst(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Category> pageList = categoryService.findByPage(page, pageSize);
        return R.success(pageList);
    }

    @DeleteMapping
    public R del(Long id){
        categoryService.delById(id);
        return R.success("删除成功");
    }

    @PutMapping
    public R edit(@RequestBody Category category){
        categoryService.update(category);
        return R.success("修改成功");
    }

    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) throws IOException {
        // 读取模板文件获取输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/cateory.xlsx");
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);
        // 获取工作空间，得到第一行
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        // 定义集合存储样式
        List<CellStyle> cellStyleList = new ArrayList<>();
        for (int i = 1; i <=5 ; i++) {
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStyleList.add(cellStyle);
        }

        // 获取数据
        List<Category> categoryList = categoryService.findAll();

        for (int i = 0; i < categoryList.size(); i++) {
            Category category = categoryList.get(i);
            // 创建行
            row = sheet.createRow(i+1);
            // 创建单元格并且设置样式
            // 类别类型
            Cell cell = row.createCell(1);
            cell.setCellStyle(cellStyleList.get(0));
            cell.setCellValue(category.getType());
            // 类别名称
            cell = row.createCell(2);
            cell.setCellStyle(cellStyleList.get(1));
            cell.setCellValue(category.getName());
            // 排序
            cell = row.createCell(3);
            cell.setCellStyle(cellStyleList.get(2));
            cell.setCellValue(category.getSort());
            // 创建时间
            cell = row.createCell(4);
            cell.setCellStyle(cellStyleList.get(3));

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));
            // 修改时间
            cell = row.createCell(5);
            cell.setCellStyle(cellStyleList.get(4));
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));
        }
        // 输出工作簿即可,通知浏览器当前是以文件下载的形式去处理内容
        response.setHeader("content-disposition", "attachment;filename=category.xlsx");
        workbook.write(response.getOutputStream());
    }

    @GetMapping("/list")
    public R list(Integer type) {
        List<Category> categoryList = categoryService.list(type);
        return R.success(categoryList);
    }
}
